package hu.rsc.feeds.client.dnd;

import hu.rsc.feeds.client.gui.DragAndDropPanel;
import hu.rsc.feeds.client.gui.RssWidget;

import com.allen_sauer.gwt.dnd.client.DragEndEvent;
import com.allen_sauer.gwt.dnd.client.DragHandler;
import com.allen_sauer.gwt.dnd.client.DragStartEvent;
import com.allen_sauer.gwt.dnd.client.VetoDragException;

public class FeedDragHandler implements DragHandler {

    private DragAndDropPanel dndPanel;

    public FeedDragHandler(DragAndDropPanel dndPanel) {
	super();
	this.dndPanel = dndPanel;
    }

    /**
     * Rollback the dnd event, if there is no droptarget.
     */
    @Override
    public void onDragEnd(DragEndEvent event) {
	FeedDropController dropController = (FeedDropController) event.getContext().finalDropController;
	RssWidget w = (RssWidget) event.getSource();
	DropTarget dt = this.dndPanel.getDropTarget(w.getUserFeed().getX().intValue(), w.getUserFeed().getY().intValue());
	if (dropController == null) {
	    dt.addContent(w);
	    return;
	} else if (((hu.rsc.feeds.client.dnd.DropTarget) dropController.getDropTarget()).getWidgetCount() != 0) {
	    dt.addContent(((hu.rsc.feeds.client.dnd.DropTarget) dropController.getDropTarget()).getContent());
	} else {
	    dt.nullContent();
	}
	((hu.rsc.feeds.client.dnd.DropTarget) dropController.getDropTarget()).addContent(w);
	System.out.println("dropcontroller: " + dropController);
    }

    @Override
    public void onDragStart(DragStartEvent event) {
	System.out.println("dragstart");
    }

    @Override
    public void onPreviewDragEnd(DragEndEvent event) throws VetoDragException {
	System.out.println("previewend");
    }

    @Override
    public void onPreviewDragStart(DragStartEvent event) throws VetoDragException {
	System.out.println("previewstart");

    }

}
